#include "HelloWorldScene.h"

#define PTM_RATIO 64

using namespace cocos2d;

HelloWorld::HelloWorld()
{
	mDir = 0;
	mAngle = 0;
}

HelloWorld::~HelloWorld()
{
	delete mWorld;
	mBody = NULL;
	mWorld = NULL;
}

CCScene* HelloWorld::scene()
{
    CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene = CCScene::node();
        CC_BREAK_IF(! scene);

        // 'layer' is an autorelease object
        HelloWorld *layer = HelloWorld::node();
        CC_BREAK_IF(! layer);

        // add layer as a child to scene
        scene->addChild(layer);
    } while (0);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {
        CC_BREAK_IF(! CCLayer::init());

		setIsTouchEnabled( true );
		CCSize winSize = CCDirector::sharedDirector()->getWinSize();

		mBall = CCSprite::spriteWithFile("Ball.png");
		mBall->setPosition(ccp(400, 300));
		addChild(mBall);

		// Create a world
		b2Vec2 gravity = b2Vec2(0.0f, 0.0f);
		bool doSleep = false;
		mWorld = new b2World(gravity, doSleep);

		// Create edges around the entire screen
		b2BodyDef groundBodyDef;
		groundBodyDef.position.Set(0,0);
		b2Body* groundBody = mWorld->CreateBody(&groundBodyDef);
		b2PolygonShape groundBox;
		groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));
		groundBody->CreateFixture(&groundBox, 0);
		groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO));
		groundBody->CreateFixture(&groundBox, 0);
		groundBox.SetAsEdge(b2Vec2(0, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO));
		groundBody->CreateFixture(&groundBox, 0);
		groundBox.SetAsEdge(b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0));
		groundBody->CreateFixture(&groundBox, 0);

		// Create ball body and shape
		b2BodyDef ballBodyDef;
		ballBodyDef.type = b2_dynamicBody;
		ballBodyDef.position.Set(400/PTM_RATIO, 300/PTM_RATIO);
		ballBodyDef.userData = mBall;
		mBody = mWorld->CreateBody(&ballBodyDef);

		b2CircleShape circle;
		circle.m_radius = 26.0/PTM_RATIO;

		b2FixtureDef ballShapeDef;
		ballShapeDef.shape = &circle;
		ballShapeDef.density = 1.0f;
		ballShapeDef.friction = 0.2f;
		ballShapeDef.restitution = 0.1f;
		mBody->CreateFixture(&ballShapeDef);

		schedule(schedule_selector(HelloWorld::tick));

        bRet = true;
    } while (0);

    return bRet;
}

void HelloWorld::tick(ccTime dt)
{
	if(mDir == 1)
	{
		mAngle += 1;
		if (mAngle > 360)
			mAngle -= 360;
	}
	else if(mDir == 2)
	{
		mAngle -= 1;
		if (mAngle < 0)
			mAngle += 360;
	}

	float a = mAngle * 3.1415926f / 180.0f;
	float x = -30 * sin(a);
	float y = -30 * cos(a);

	mWorld->SetGravity(b2Vec2(x,y));

	mWorld->Step(dt, 2, 2);
	for(b2Body* b = mWorld->GetBodyList(); b; b=b->GetNext())
	{
		if (b->GetUserData() != NULL)
		{
			CCSprite* ballData = (CCSprite*)b->GetUserData();
			float x = b->GetPosition().x * PTM_RATIO;
			float y = b->GetPosition().y * PTM_RATIO;
			ballData->setPosition(ccp(x,y));
			ballData->setRotation(-1 * CC_RADIANS_TO_DEGREES(b->GetAngle()));
		}        
	}
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    // "close" menu item clicked
    CCDirector::sharedDirector()->end();
}

void HelloWorld::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
	CCTouch* touch = (CCTouch*)pTouches->anyObject();

	if(!touch)
		return;

	mTouch = touch;

	CCPoint location = touch->locationInView(touch->view());
	location = CCDirector::sharedDirector()->convertToGL(location);

	if( location.x < 320 )
		mDir = 1;
	else
		mDir = 2;

}

void HelloWorld::ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
	CCTouch* touch = (CCTouch*)pTouches->anyObject();

	if( touch == mTouch )
	{
		mDir = 0;
		mTouch = NULL;
	}
}

/*
bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
	CCPoint location = pTouch->locationInView(pTouch->view());
	location = CCDirector::sharedDirector()->convertToGL(location);

	if( location.x < 320 )
		mDir = 1;
	else
		mDir = 2;

	return true;
}

void HelloWorld::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
	mDir = 0;
}
*/
/*
void HelloWorld::onEnter()
{
	CCTouchDispatcher::sharedDispatcher()->addStandardDelegate(this,0);
	CCLayer::onEnter();
}

void HelloWorld::onExit()
{
	CCTouchDispatcher::sharedDispatcher()->removeDelegate(this);
	CCLayer::onExit();
}
*/